package com.basker.pisces.domain.entity.event;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.Assert;

import com.basker.pisces.domain.entity.data.IDataEntity;

/**
 * 数据实体事件发布器，同步负责管理事件订阅者
 *
 * @author hangwen
 */
public class DataEntityEventPublisher {

    private IDataEntity dataEntity;
    private List<IDataEntitySubcriber> subcribers = new ArrayList<>();

    public DataEntityEventPublisher(IDataEntity dataEntity) {
        this.dataEntity = dataEntity;
    }

    public void addSubcriber(IDataEntitySubcriber subcriber) {
        Assert.notNull(subcriber, "parameter 'subcriber' is required");
        this.subcribers.add(subcriber);
    }

    public void publishDataObjectCreateEvent() {
        DataObjectCreateEvent event = new DataObjectCreateEvent(dataEntity);

        for (IDataEntitySubcriber subcriber : subcribers) {
            subcriber.onDataObjectCreated(event);
        }
    }

    public void removeSubcriber(IDataEntitySubcriber subcriber) {
        Assert.notNull(subcriber, "parameter 'subcriber' is required");
        this.subcribers.remove(subcriber);
    }

}
